function precodingMatrix = WiSiL_Precoder_codebook ( scheme )
% WISIL_PRECODER_CODEBOOK 
%
%   WiSiL_Precoder_codebook( SCHEME ) returns the precoding matrices for
%   the multiple antenna scheme in SCHEME, which is either 'multiplexing' 
%   or 'diversity'
%   

% History:
% ?????, ?????
%   created


% Codebook with the precoding matrixes. 
if strcmpi(scheme,'multiplexing')

    if txNum == 2;

        precodingMatrix = cell(6,2);
        precodingMatrix(1,1)={[1;0]};
        precodingMatrix(2,1)={[0; 1]};
        precodingMatrix(3,1)={(1/sqrt(2))*[1; 1]};
        precodingMatrix(4,1)={(1/sqrt(2))*[1; -1]};
        precodingMatrix(5,1)={(1/sqrt(2))*[1; 1i]};
        precodingMatrix(6,1)={(1/sqrt(2))*[1; -1i]};
        precodingMatrix(1,2) = {(1/sqrt(2))*[1 0; 0 1]};
        precodingMatrix(2,2) = {(1/2)*[1 1; 1 -1]};
        precodingMatrix(3,2) = {(1/2)*[1 1; 1i -1i]};

    elseif txNum == 4;
        precodingMatrix=cell(16,4);
        u(:,1)=[1 -1 -1 -1];
        u(:,2)=[1 -1i 1 1i];
        u(:,3)=[1 1 -1 1];
        u(:,4)=[1 1i 1 -1i];
        u(:,5)=[1 (-1-1i)/sqrt(2) -1i (1-1i)/sqrt(2)];
        u(:,6)=[1 (1-1i)/sqrt(2) 1i (-1-1i)/sqrt(2)];
        u(:,7)=[1 (1+1i)/sqrt(2) -1i (-1+1i)/sqrt(2)];
        u(:,8)=[1 (-1+1i)/sqrt(2) 1i (1+1i)/sqrt(2)];
        u(:,9)=[1 -1 1 1];
        u(:,10)=[1 -1i -1 -1i];
        u(:,11)=[1 1 1 -1];
        u(:,12)=[1 1i -1 1i];
        u(:,13)=[1 -1 -1 1];
        u(:,14)=[1 -1 1 -1];
        u(:,15)=[1 1 -1 -1];
        u(:,16)=[1 1 1 1];

        for k=1:16
            auxiliar{k,1}=eye(4)-2*u(:,1)*u(:,1)'/(u(:,1)'*u(:,1));
            precodingMatrix{k,1}=auxiliar{k,1}(:,1);
            
        end
        
        
        
        precodingMatrix{1,2}=[auxiliar{1,1}(:,1) auxiliar{1,1}(:,4)] ;
        precodingMatrix{2,2}=auxiliar{2,1}(:,1:2);
        precodingMatrix{3,2}=auxiliar{3,1}(:,1:2);
        precodingMatrix{4,2}=auxiliar{4,1}(:,1:2);
        precodingMatrix{5,2}=auxiliar{5,1}(:,1:2);
        precodingMatrix{6,2}=[auxiliar{6,1}(:,1) auxiliar{6,1}(:,4)];
        precodingMatrix{7,2}=[auxiliar{7,1}(:,1) auxiliar{7,1}(:,3)];
        precodingMatrix{8,2}=[auxiliar{8,1}(:,1) auxiliar{8,1}(:,3)];
        precodingMatrix{9,2}=auxiliar{9,1}(:,1:2);
        precodingMatrix{10,2}=[auxiliar{10,1}(:,1) auxiliar{10,1}(:,4)];
        precodingMatrix{11,2}=[auxiliar{11,1}(:,1) auxiliar{11,1}(:,3)];
        precodingMatrix{12,2}=[auxiliar{12,1}(:,1) auxiliar{12,1}(:,3)];
        precodingMatrix{13,2}=auxiliar{13,1}(:,1:2);
        precodingMatrix{14,2}=[auxiliar{14,1}(:,1) auxiliar{14,1}(:,3)];
        precodingMatrix{15,2}=[auxiliar{15,1}(:,1) auxiliar{15,1}(:,3)];
        precodingMatrix{16,2}=auxiliar{16,1}(:,1:2);

        precodingMatrix{1,3}=[auxiliar{1,1}(:,1:2) auxiliar{1,1}(:,4)] ;
        precodingMatrix{2,3}=auxiliar{2,1}(:,1:3);
        precodingMatrix{3,3}=auxiliar{3,1}(:,1:3);
        precodingMatrix{4,3}=auxiliar{4,1}(:,1:3);
        precodingMatrix{5,3}=[auxiliar{5,1}(:,1:2) auxiliar{5,1}(:,4)];
        precodingMatrix{6,3}=[auxiliar{6,1}(:,1:2) auxiliar{6,1}(:,4)];
        precodingMatrix{7,3}=[auxiliar{7,1}(:,1) auxiliar{7,1}(:,3:4)];
        precodingMatrix{8,3}=[auxiliar{8,1}(:,1) auxiliar{8,1}(:,3:4)];
        precodingMatrix{9,3}=[auxiliar{9,1}(:,1:2) auxiliar{9,1}(:,4)];
        precodingMatrix{10,3}=[auxiliar{10,1}(:,1) auxiliar{10,1}(:,3:4)];
        precodingMatrix{11,3}=auxiliar{11,1}(:,1:3);
        precodingMatrix{12,3}=[auxiliar{12,1}(:,1) auxiliar{12,1}(:,3:4)];
        precodingMatrix{13,3}=auxiliar{13,1}(:,1:3);
        precodingMatrix{14,3}=auxiliar{14,1}(:,1:3);
        precodingMatrix{15,3}=auxiliar{15,1}(:,1:3);
        precodingMatrix{16,3}=auxiliar{16,1}(:,1:3);

        precodingMatrix{1,4}=auxiliar{1,1}(:,1:4) ;
        precodingMatrix{2,4}=auxiliar{2,1}(:,1:4);
        precodingMatrix{3,4}=[ auxiliar{3,1}(:,3) auxiliar{3,1}(:,2) ...
                               auxiliar{3,1}(:,1) auxiliar{3,1}(:,4) ];
        precodingMatrix{4,4}=[ auxiliar{4,1}(:,3) auxiliar{4,1}(:,2) ...
                               auxiliar{4,1}(:,1) auxiliar{4,1}(:,4)];
        precodingMatrix{5,4}=auxiliar{5,1}(:,1:4);
        precodingMatrix{6,4}=auxiliar{6,1}(:,1:4);
        precodingMatrix{7,4}=[ auxiliar{7,1}(:,1) auxiliar{7,1}(:,3) ...
                               auxiliar{7,1}(:,2) auxiliar{7,1}(:,4)];
        precodingMatrix{8,4}=[ auxiliar{8,1}(:,1) auxiliar{8,1}(:,3) ...
                               auxiliar{8,1}(:,2) auxiliar{8,1}(:,4)];
        precodingMatrix{9,4}=auxiliar{9,1}(:,1:4);
        precodingMatrix{10,4}=auxiliar{10,1}(:,1:4);
        precodingMatrix{11,4}=[ auxiliar{11,1}(:,1) auxiliar{11,1}(:,3) ...
                                auxiliar{11,1}(:,2) auxiliar{11,1}(:,4)];
        precodingMatrix{12,4}=[ auxiliar{12,1}(:,1) auxiliar{12,1}(:,3) ...
                                auxiliar{12,1}(:,2) auxiliar{12,1}(:,4)];
        precodingMatrix{13,4}=auxiliar{13,1}(:,1:4);
        precodingMatrix{14,4}=[ auxiliar{14,1}(:,1) auxiliar{14,1}(:,3) ...
                                auxiliar{14,1}(:,2) auxiliar{14,1}(:,4)];
        precodingMatrix{15,4}=[ auxiliar{15,1}(:,3) auxiliar{15,1}(:,2) ...
                                auxiliar{15,1}(:,1) auxiliar{15,1}(:,4)];
        precodingMatrix{16,4}=auxiliar{16,1}(:,1:4);

    end

elseif strcmpi(scheme,'diversity')
    
    if layers==2
        
        precodingMatrix =( 1/sqrt(2) ) *[1 0 1i 0; 0 -1 0 1i; 0 1 0 1i; 1 0 -1i 0];
        
    elseif layers==4
        
        precodingMatrix =( 1/sqrt(2) ) *[1 0 0 0 1i 0 0 0; 0 0 0 0 0 0 0 0;...
                                        0 -1 0 0 0 1i 0 0; 0 0 0 0 0 0 0 0;...
                                        0 1 0 0 0 1i 0 0; 0 0 0 0 0 0 0 0;...
                                        1 0 0 0 -1i 0 0 0; 0 0 0 0 0 0 0 0;...
                                        0 0 0 0 0 0 0 0; 0 0 1 0  0 0 1i 0;...
                                        0 0 0 0 0 0 0 0; 0 0 0 -1 0 0 0 1i;...
                                        0 0 0 0 0 0 0 0; 0 0 0 1 0 0 0 1i; ...
                                        0 0 0 0 0 0 0 0; 0 0 1 0 0 0 -1i 0];
    
    end

else
    precodingMatrix = 1;
end